//
// QSDropDownListView.m
// myTest
//
// Created by Song on 2017/10/26.
// Copyright © 2017年 Song. All rights reserved.
//

#import "QSDropDownListView.h"

#import "QSDropDownListModel.h"

@interface QSDropDownListView ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) QSDropDownListModel *defaultModel;
@property (nonatomic, strong) QSDropDownListModel *listModel;
@property (nonatomic, strong) NSArray *dataArray;

@property (nonatomic, weak) UIView *senderView;
@property (nonatomic, weak) UITableView *tableView;
@end

@implementation QSDropDownListView
- (instancetype)initDropDownListViewWithSenderView:(UIView *)senderView dataArray:(NSArray<NSString *> *)dataArray andViewModel:(QSDropDownListModel *)model
{
    if (self = [super init]) {
        self.frame = [UIScreen mainScreen].bounds;
        
        self.senderView = senderView;
        
        // 初始化model
        [self initDefaultModel];
        
        self.dataArray = dataArray;
        self.listModel = model;
        
        // 创建TableView
        [self createTableView];
    }
    return self;
}

#pragma mark - System Delegate
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray .count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* indentifier = @"QSDropDownCellID";
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:indentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
    }
    cell.textLabel.font = self.listModel.textFont;
    switch (self.listModel.textStyle) {
        case QSTextStayStyle_Left:
            cell.textLabel.textAlignment = NSTextAlignmentLeft;
            break;
            
        case QSTextStayStyle_Middle:
            cell.textLabel.textAlignment = NSTextAlignmentCenter;
            break;
            
        case QSTextStayStyle_Right:
            cell.textLabel.textAlignment = NSTextAlignmentRight;
            break;
    }
    
    cell.textLabel.textColor = self.listModel.textColor;
    cell.textLabel.text = self.dataArray[indexPath.row];
    cell.contentView.backgroundColor = self.listModel.listViewBgColor;
    
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row != (self.dataArray.count - 1)) {
        if ((self.listModel.needRightLine && self.listModel.needLeftLine)) {
            if (indexPath.row != (self.dataArray.count - 2)) {
                UIView *separatorView = [[UIView alloc] initWithFrame:CGRectMake(0, cell.frame.size.height - 1, cell.frame.size.width, 1)];
                separatorView.backgroundColor = self.listModel.separatorColor;
                [cell.contentView addSubview:separatorView];
            }
        } else {
            UIView *separatorView = [[UIView alloc] initWithFrame:CGRectMake(0, cell.frame.size.height - 1, cell.frame.size.width, 1)];
            separatorView.backgroundColor = self.listModel.separatorColor;
            [cell.contentView addSubview:separatorView];
        }
        
        if (self.listModel.needLeftLine) {
            UIView *leftLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1.0, cell.frame.size.height)];
            leftLine.backgroundColor = self.listModel.separatorColor;
            [cell.contentView addSubview:leftLine];
        }
        
        if (self.listModel.needRightLine) {
            UIView *rightLine = [[UIView alloc] initWithFrame:CGRectMake(cell.frame.size.width - 1, 0, 1.0, cell.frame.size.height)];
            rightLine.backgroundColor = self.listModel.separatorColor;
            [cell.contentView addSubview:rightLine];
        }
    } else {
        if (self.listModel.needRightLine && self.listModel.needLeftLine) {
            UIView *lineView = [[UIView alloc] initWithFrame:cell.bounds];
            [cell.contentView addSubview:lineView];
            
            CAShapeLayer *lineBorder  = [[CAShapeLayer alloc] init];
            lineBorder.frame = cell.bounds;
            [lineBorder setLineWidth:2];
            [lineBorder setStrokeColor:self.listModel.separatorColor.CGColor];
            [lineBorder setFillColor:self.listModel.listViewBgColor.CGColor];
            
            UIRectCorner rectCor = UIRectCornerAllCorners;
            
            if (self.listModel.isBottomCornerRadius) {
                rectCor = UIRectCornerBottomLeft | UIRectCornerBottomRight;
            }
            
            UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:cell.bounds byRoundingCorners:rectCor cornerRadii:CGSizeMake(self.listModel.listViewCornerRadius, self.listModel.listViewCornerRadius)];
            lineBorder.path = path.CGPath;
            [lineView.layer addSublayer:lineBorder];
            [cell.contentView sendSubviewToBack:lineView];
        }
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (self.selectedRowBlock) {
        self.selectedRowBlock(self, indexPath.row);
    }
    [self hideDropDownListView];
}

#pragma mark - Private Methods
/**
 *  创建TableView
 */
- (void)createTableView
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(self.listModel.listViewStartX, self.listModel.listViewStartY, self.listModel.listViewWidth, 0.0)];
    self.tableView = tableView;
    
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.separatorColor = self.listModel.separatorColor;
    tableView.backgroundColor = self.listModel.listViewBgColor;
    tableView.rowHeight = self.listModel.listViewRowHeight;
    tableView.bounces = NO;
    
    [self addSubview:tableView];
}

/**
 *  初始化model
 */
- (void)initDefaultModel
{
    self.defaultModel = [[QSDropDownListModel alloc] init];
    self.defaultModel.bgViewColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
    self.defaultModel.listViewCornerRadius = 5.0;
    self.defaultModel.listViewWidth = self.senderView.frame.size.width;
    self.defaultModel.listViewRowHeight = 44.0;
    self.defaultModel.textFont = [UIFont systemFontOfSize:15.0];
    self.defaultModel.textStyle = QSTextStayStyle_Left;
    self.defaultModel.textColor = [UIColor blackColor];
    self.defaultModel.listViewBgColor = [UIColor whiteColor];
    self.defaultModel.separatorColor = [UIColor lightGrayColor];
    self.defaultModel.visibleMaxCount = 4;
    self.defaultModel.isBottomCornerRadius = NO;
}

- (void)showDropDownListView
{
    UIWindow *qs_window = [UIApplication sharedApplication].keyWindow;
    [qs_window addSubview:self];
    
    CGFloat listViewHeight = self.listModel.listViewRowHeight * self.listModel.visibleMaxCount;
    [UIView animateWithDuration:0.5 animations:^{
        self.tableView.frame = CGRectMake(self.listModel.listViewStartX, self.listModel.listViewStartY, self.listModel.listViewWidth, listViewHeight);
    } completion:^(BOOL finished) {
    }];
}

- (void)hideDropDownListView
{
    [UIView animateWithDuration:0.5 animations:^{
        self.tableView.frame = CGRectMake(self.listModel.listViewStartX, self.listModel.listViewStartY, self.listModel.listViewWidth, 0.0);
    } completion:^(BOOL finished) {
        [self.tableView removeFromSuperview];
        [self removeFromSuperview];
    }];
}

#pragma mark - System Methods
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self hideDropDownListView];
}

#pragma mark - Setter
- (void)setListModel:(QSDropDownListModel *)listModel
{
    if (!listModel.bgViewColor) {
        listModel.bgViewColor = self.defaultModel.bgViewColor;
    }
    
    if (listModel.listViewCornerRadius <= 0.0) {
        listModel.listViewCornerRadius = self.defaultModel.listViewCornerRadius;
    }
    
    if (!listModel.isBottomCornerRadius) {
        listModel.isBottomCornerRadius = self.defaultModel.isBottomCornerRadius;
    }
    
    if (listModel.listViewWidth <= 0.0) {
        listModel.listViewWidth = self.defaultModel.listViewWidth;
    }
    
    if (listModel.listViewRowHeight <= 0.0) {
        listModel.listViewRowHeight = self.defaultModel.listViewRowHeight;
    }
    
    if (!listModel.textFont) {
        listModel.textFont = self.defaultModel.textFont;
    }
    
    if (!listModel.textColor) {
        listModel.textColor = self.defaultModel.textColor;
    }
    
    if (!listModel.listViewBgColor) {
        listModel.listViewBgColor = self.defaultModel.listViewBgColor;
    }
    
    if (!listModel.separatorColor) {
        listModel.separatorColor = self.defaultModel.separatorColor;
    }
    
    if (listModel.visibleMaxCount <= 0) {
        listModel.visibleMaxCount = self.defaultModel.visibleMaxCount;
    }
    if (self.dataArray.count < listModel.visibleMaxCount) {
        listModel.visibleMaxCount = self.dataArray.count;
    }
    
    self.backgroundColor = listModel.bgViewColor;
    _listModel = listModel;
}
@end
